//
//  XMLoadStatusTool.m
//  XMWeibo
//
//  Created by chi on 14-8-30.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMLoadStatusTool.h"
#import "XMHttpTool.h"
#import "XMLoadCacheStatusTool.h"

@implementation XMLoadStatusTool



#pragma mark - 通过http加载微博
/**
 *  加载微博
 *
 *  @param params  请求参数
 *  @param success 成功回调
 *  @param failure 失败回调
 */
+ (void)loadStatusWithParams:(XMHttpGetStatusParams *)params success:(void (^)(XMHttpGetStatusResult *))success failure:(void (^)(NSError *))failure
{
    XMHttpGetStatusResult *sqliteResult = [self sqliteLoadStatusWithParams:params];
    
    if (sqliteResult.statuses.count > 0) {
        success(sqliteResult);
    }
    else {
        
        [XMHttpTool httpGetWithURL:k_statuses_home_timeline params:params.keyValues success:^(id responseObject) {
            // 请求成功
            XMHttpGetStatusResult *result = [XMHttpGetStatusResult objectWithKeyValues:responseObject];
            
            // 缓存到sqltite
            [XMLoadCacheStatusTool saveStatusWithParam:result access_token:params.access_token];
            
            success(result);
            
        } failure:^(NSError *error) {
            // 请求失败
            failure(error);
            
        }];
    }
    
}


#pragma mark - 通过sqlite加载微博
+ (XMHttpGetStatusResult *)sqliteLoadStatusWithParams:(XMHttpGetStatusParams *)params
{
    return [XMLoadCacheStatusTool loadStatusWithParam:params];
}


@end
